#include "terrain.h"
#include <lampe/xml/IXMLFactoryEnvironment.h>
#include "irrxml.h"
#include <lampe/xml/IXmlNode.h>
#include <ISceneNodeAnimator.h>
#include <ISceneManager.h>
#include <ITerrainSceneNode.h>
#include <IFileSystem.h>
#include "utils/base_scenenode.h"

namespace lampe{
namespace xml{
class TerrainFactory:public IIrrXmlNode{
public:
	TerrainFactory(irr::IrrlichtDevice*d):IIrrXmlNode(d){}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> list;
		list.push_back("terraine");
		return list;
	}
	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		irr::scene::ISceneNode*parent=0;
		if (node->getAttString("href","")==""){
			return lampe::xml::STOP;
		}
			
		if ((node->getParent()) && (node->getParent()->getTag()))
			parent=(irr::scene::ISceneNode*)node->getParent()->getTag();

		irr::scene::ITerrainSceneNode*terrain=getDevice()->getSceneManager()->addTerrainSceneNode(
	    		node->getAttString("href","").c_str(),
	            parent,                                      //родительский узел
	            -1,                                     //id узла
	            irr::core::vector3df(0.f, 0.f, 0.f),         //позиция
	            irr::core::vector3df(0.f, 0.f, 0.f),         //поворот
	            irr::core::vector3df(1.0f,1.0f,1.0f),//маштаб
	            node->getAttColor("color",255,255,255,255),   //цвет вершин
	            node->getAttS32("maxlod",4),                                      //максимум LOD
	            (irr::scene::E_TERRAIN_PATCH_SIZE)node->getAttU32("patch",9),                         //размер патча
	            node->getAttS32("smooth",4),                                       //коэф-т размытия
	            true);
		irr::io::IReadFile*img_file=getDevice()->getFileSystem()->createAndOpenFile(node->getAttString("href","").c_str());
		terrain->loadHeightMap(img_file);
		img_file->drop();

		irr::core::vector2df texture_scale=node->getAttVec2F("texture_scale",1.f,1.f);
		terrain->scaleTexture(texture_scale.X,texture_scale.Y);
		readBaseSceneNodeTags(terrain,node);
		node->setTag(terrain);
		return lampe::xml::NEXT;
	}

	virtual lampe::xml::TagResult tagHandlerAfter(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		/*
		irr::scene::ITerrainSceneNode*terrain=(irr::scene::ITerrainSceneNode*)node->getTag();
		for (int i=0;i<terrain->getMaterialCount();i++){
			irr::u8 count=0;
			for (int j=0;j<_IRR_MATERIAL_MAX_TEXTURES_;j++)
				if (terrain->getMaterial(i).getTexture(j))
					count++;
			irr::s32 shader_id=0;
			if (count>=3)
				shader_id=environment->getDevice()->getVideoDriver()->getGPUProgrammingServices()->addLayoutShader(count-2);
			if (shader_id!=0)
				terrain->getMaterial(i).MaterialType=((irr::video::E_MATERIAL_TYPE)shader_id);
			return irr::xml::NEXT;
		}
		*/
		return lampe::xml::NEXT;
	}
};

}//namespace xml
}//namespace lampe
		

void regTerrain(lampe::xml::IXMLFactoryEnvironment*environment,irr::IrrlichtDevice*d){
	lampe::xml::IFactoryRecord*h=new lampe::xml::TerrainFactory(d);
	environment->registerHandler(h);
	h->drop();
}